package com.zzx.threekingdom.model;

import com.zzx.threekingdom.card.Card;
import com.zzx.threekingdom.card.CardList;
import com.zzx.threekingdom.card.EquipmentCard;
import com.zzx.threekingdom.view.PlayerView;
import com.zzx.threekingdom.judge.Judge.IAskYesNo;
import com.zzx.threekingdom.judge.Judge.IPassAction;
import com.zzx.threekingdom.judge.Judge.IPassCard;
import com.zzx.threekingdom.judge.Judge.IPassCards;


import java.util.ArrayList;
import java.util.Random;

public class AiPlayerModel extends AbsPlayerModel
{
    public PlayerView mPlayerView = null;
    public UserPlayerModel mPlayerModel = null;
    
    private final static int USE_STEP_EQUIP = 1;
    private final static int USE_STEP_PEACH = 2;
    private final static int USE_STEP_KONWHOW = 3;
    private final static int USE_STEP_BASE = 4;
    private final static int USE_STEP_FINAL = 5;
    
    private int CurrentStep = 0;
    
    
    public AiPlayerModel(PlayerView playerView)
    {
        super(playerView);
    }
    
    public void pickTargetOneCard(ArrayList<CardList> cardss, IPassCard i)
    {
    	Random randomObj = new Random();
        int num = cardss.size();
        if(num>0)
        {
        	CardList cards = cardss.get(randomObj.nextInt(num - 1));
        	num = cards.size();
            i.onGotCard(cards.get(randomObj.nextInt(num - 1)));
        }
    }
    
    public void pickOneCharacter(CardList characterCards, final IPassCard i)
    {
    	Random randomObj = new Random();
        int num = characterCards.size();
        if(num>0)
        {
            i.onGotCard(characterCards.get(randomObj.nextInt(num - 1)));
        }
    }

    public void selectAbondonCards(int num, IPassCards i)
    {
        i.onGotCards(getHandCards().getCards(num));
    }
    
    public void onNeedActionByName(String name, IPassCard ipc, IPassAction ipa, ActionInfo askInfo)
    {
        if(name.equals("桃"))
        {
            if((askInfo!=null)&&(askInfo.getActionName().equals("求桃"))&&(askInfo.getTo()!=this))
            {
                ipc.onGotCard(null);
                return;
            }
        }
        else if(name.equals("无懈可击"))
        {
            if((askInfo!=null)&&(askInfo.getFrom()==this))
            {
                ipc.onGotCard(null);
                return;
            }
        }
        
        for(int i = 0;i < handCards.size();i++)
        {
            Card card = handCards.get(i); 
            if(card.getName().equals(name)==true)
            {
                ipc.onGotCard(card);
                return;
            }
        }
        
        ipc.onGotCard(null);
    }
    
    public void askYesNo(String ask, IAskYesNo ia)
    {
    	ia.onResult(true);
    }
    
    
    
    public void onActiveUseCards()
    {
        CurrentStep = 1;
        
        CardList usedCards = new CardList();
        while(usedCards.containsCards(handCards)==false)
        {
            Card card = null;
            
            for(int i = 0;i < handCards.size();i++)
            {
                Card tmpCard = handCards.get(i);

                if(usedCards.contains(tmpCard)==false)
                {
                    usedCards.add(tmpCard);
                    aiActiveUseCard(tmpCard);
                    card = tmpCard;
                    break;
                }
            }
            if(card==null)
                break;
        }

        enterAbandonCards();
    }
    
    private void aiActiveUseCard(Card card)
    {
        if(card instanceof EquipmentCard)
        {
            activeUseCard(card, null, null);
        }
    }
    

}
